const express = require('express');
const fs = require('fs');

const app = express();

app.use(express.static('./'));
let html = fs.readFileSync('./index.html', 'utf-8');
html = html.replace(/window.SERVICE="[^"]*"/, `window.SERVICE="${process.env.REACT_APP_SERVICE}"`);

app.use('*/static/js/*', (req, res, next) => {
  console.log('here', req.originalUrl);
  res.redirect(req.originalUrl.replace(/^.*static\//, '/static/'));
});

app.use(['/', '/index.html', '/index'], (req, res, next) => {
  res.status(200);
  res.send(html);
});

app.listen(process.env.PORT, () => {
  console.log('server...');
});
